<template>
  <download-manage-top />
  <download-manage-side-bar />
  <download-manage-content />
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import useSetWindowDragSize from '@/hooks/use-set-window-drag-size'
import { invokeAsyncAPi, registerInformApi} from '@/utils/xz-fn'
import { useDownloadManage1 } from '@/stores/download-manage1'
import { useUserInfo1} from '@/stores/user-info1'
import { useDownloadManageWindow } from '@/stores/download-manage-window'
import useDialogPromiseHook from '@/hooks/use-dialog-promise-hook'
import { useDownloadManageMenu } from '@/stores/download-manage-menu'
import { storeToRefs } from 'pinia'

const downloadManageStore = useDownloadManage1();
const userInfoStore = useUserInfo1();
const downloadManageWindowStore = useDownloadManageWindow();
const {masterWindowId }=storeToRefs(downloadManageWindowStore)
const dialog = useDialogPromiseHook()
const downloadManageMenuStore = useDownloadManageMenu()

useSetWindowDragSize();
registerInformApi('Notify_WebMessage', (windowId: string, type: string, data: any) => {
  if (type === 'showWindow') {
    invokeAsyncAPi('jsMsg_SetWndStatus',6)
  } else if (type === 'closeWindow') {
    invokeAsyncAPi('jsMsg_SetWndStatus',0)
  } else if (type === 'download-manage-downing-table-list') {
    downloadManageStore.setDowningList(data);
  } else if (type === 'download-manage-finish-table-list') {
    downloadManageStore.setFinishList(data);
  } else if (type==='download-manage-finish-red-flag') {
    downloadManageStore.setDownloadFinishRedFlag(data);
  } else if (type==='download-manage-is-member') {
    userInfoStore.setIsMember(data)
  } else if (type === 'download-finish-success') {
    dialog.showDialog('dialogSimple', {
      content: data + '下载成功\n可以在“下载记录”里进行查看',
      success:true
    }).then(() => {
    }).catch(() => {})
  } else if (type === 'download-finish-fail') {
    dialog.showDialog('downloadError', data).then(() => {
    }).catch(() => {})
  } else if (type==='download-user-overload') {
    dialog.showDialog('userOverload').then((value) => {
      invokeAsyncAPi('fq_ShowUserCenter')
      invokeAsyncAPi('fq_NotifyWebMessageEx', masterWindowId.value, 'continue-user-overload-dialog', data);
      invokeAsyncAPi('fq_NotifyWebMessageEx', masterWindowId.value, 'close-fake-download-dialog');
    }).catch((value) => {
      if (value === 'member') {
          // invokeAsyncAPi('fq_NotifyWebMessageEx', masterWindowId.value, 'continue-user-overload-dialog',data);
          invokeAsyncAPi('fq_ShowUserCenter')
      }
      invokeAsyncAPi('fq_NotifyWebMessageEx', masterWindowId.value, 'close-fake-download-dialog');
    })
  }
})

const getCurrentWindowId = async () => {//获取当前窗口id
  const id = await invokeAsyncAPi('jsMsg_GetWndID');//获取系统最大磁盘
  downloadManageWindowStore.setDownloadManageWindowId(id);
}

const getMasterWindowId = async () => {//获取主窗口id
  const data = await invokeAsyncAPi('jsMsg_GetWndInfo');//获取系统最大磁盘
  if (data.payload) {
    const _data = JSON.parse(data.payload)
    downloadManageWindowStore.setMasterWindowId(_data.id);
    downloadManageMenuStore.setActiveMenu(_data.tab??'1')
    //向主窗口请求下载中的table数据
    invokeAsyncAPi('fq_NotifyWebMessageEx', _data.id, 'get-downing-table-list');
    //向主窗口请求下载完成的table数据
    invokeAsyncAPi('fq_NotifyWebMessageEx', _data.id, 'get-download-finish-table-list');
    //向主窗口请求当前下载完成小红点
    invokeAsyncAPi('fq_NotifyWebMessageEx', _data.id, 'get-download-finish-red-flag');
    //向主窗口请求当前是否是会员
    invokeAsyncAPi('fq_NotifyWebMessageEx', _data.id, 'get-download-is-member');
  }
}

onMounted(() => {
  getCurrentWindowId();//获取主窗口id
  getMasterWindowId();//得到主窗口id
})
</script>

<style scoped lang="scss">
</style>